import { request } from '../request';

/**
 * Login
 *
 * @param userName User name
 * @param password Password
 * @param imgCode 验证码
 * @param imgToken imgToken
 */
export function fetchLogin(userName: string, password: string, imgCode: string, imgToken: string) {
  return request<Api.Auth.LoginToken>({
    url: '/admin/auth/login',
    method: 'post',
    data: {
      userName,
      password,
      imgCode,
      imgToken
    }
  });
}
/**
 * 图片验证码
 */
export function getImgCode() {
  return request<Api.Auth.ImgCode>({
    url: '/admin/auth/captcha',
    method: 'get'
  });
}

/** user logout */
export function fetchLogout() {
  return request<boolean>({
    url: '/admin/auth/logout',
    method: 'get'
  });
}

/** Get user info */
export function fetchGetUserInfo() {
  return request<Api.Auth.UserInfo>({ url: '/admin/auth/user_info' });
}

/**
 * Refresh token
 *
 * @param refreshToken Refresh token
 */
export function fetchRefreshToken(refreshToken: string) {
  return request<Api.Auth.LoginToken>({
    url: '/auth/refresh_token',
    method: 'post',
    data: {
      refreshToken
    }
  });
}

/**
 * return custom backend error
 *
 * @param code error code
 * @param msg error message
 */
export function fetchCustomBackendError(code: string, msg: string) {
  return request({ url: '/auth/error', params: { code, msg } });
}
